package com.duoduo.demo.springnoxml.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

/**
 * HttpClientUtils
 * @author chengesheng@kedacom.com
 * @date 2017年11月29日下午3:54:38
 */
public class HttpClientUtils {

	public static final String UTF8 = "utf-8";

	/**
	 * 发起Get请求
	 */
	public static String doGet(String url) throws IOException {
		return doGet(url, UTF8);
	}

	/**
	 * 发起Get请求
	 */
	public static String doGet(String url, String encoding) throws IOException {
		HttpClient httpClient = getDefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		HttpResponse response = httpClient.execute(httpGet);
		return readResponse(response, encoding);
	}

	/**
	 * 发起Post请求
	 */
	public static String doPost(String url, String param) throws IOException {
		return doPost(url, UTF8, param);
	}

	/**
	 * 发起Post请求
	 */
	public static String doPost(String url, String encoding, String param) throws IOException {
		HttpClient httpClient = getDefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		StringEntity reqEntity = new StringEntity(param, UTF8);
		httpPost.setEntity(reqEntity);
		HttpResponse response = httpClient.execute(httpPost);
		return readResponse(response, encoding);
	}

	/**
	 * 发起Post请求，多个参数（Form表单参数）
	 */
	public static String doPost(String url, Map<String, String> params) throws IOException {
		List<BasicNameValuePair> paramList = new ArrayList<BasicNameValuePair>();
		for (Map.Entry<String, String> param : params.entrySet()) {
			paramList.add(new BasicNameValuePair(param.getKey(), param.getValue()));
		}
		return doPost(url, UTF8, paramList);
	}

	/**
	 * 发起Post请求，多个参数（Form表单参数）
	 */
	public static String doPost(String url, BasicNameValuePair... params) throws IOException {
		return doPost(url, UTF8, Arrays.asList(params));
	}

	/**
	 * 发起Post请求，多个参数（Form表单参数）
	 */
	public static String doPost(String url, List<BasicNameValuePair> params) throws IOException {
		return doPost(url, UTF8, params);
	}

	/**
	 * 发起Post请求，多个参数（Form表单参数）
	 */
	public static String doPost(String url, String encoding, List<BasicNameValuePair> params) throws IOException {
		HttpClient httpClient = getDefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		HttpEntity httpEntity = new UrlEncodedFormEntity(params, encoding);
		httpPost.setEntity(httpEntity);
		HttpResponse response = httpClient.execute(httpPost);
		return readResponse(response, encoding);
	}

	/**
	 * 读取相应值，非200状态返回null
	 */
	private static String readResponse(HttpResponse response, String encoding) throws IOException {
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			StringBuffer result = new StringBuffer();
			HttpEntity httpEntity = response.getEntity();
			InputStream inputStream = httpEntity.getContent();
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, encoding));
			String readLine = null;
			while ((readLine = bufferedReader.readLine()) != null) {
				result.append(readLine);
			}
			inputStream.close();
			bufferedReader.close();
			return result.toString();
		} else {
			System.err.println("非200，状态值=" + response.getStatusLine().getStatusCode());
			return null;
		}
	}

	public static CloseableHttpClient getDefaultHttpClient() {
		return HttpClientBuilder.create().build();
	}
}
